#!/usr/bin/env python

#
# OpenLookup - test.py
#
# Copyright 2008 Helsinki Institute for Information Technology
# and the authors.
#
# Authors: Ken Rimey <rimey@hiit.fi>
#

# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

from hashlib import sha1

from openlookup.client.multivalued import Proxy

URL = 'http://localhost:5851/'
#URL = 'http://any.openlookup.net:5851/'
#URL = 'http://openlookup.appspot.com/'
#URL = 'http://opendht.nyuld.net:5851/'

PRERESOLVE = True # Set this to False for virtual hosts (e.g., App Engine).

class Test(Proxy):
    def test(self):
        self.test_get()
        self.test_get_details()
        self.test_put()
        self.test_rm()

    def test_get(self):
        print 'test_get()'
        self.put('shape', 'round')
        self.put('shape', 'square')
        self.put('shape', 'square')
        self.put('shape', 'square', 'xyzzy')
        self.put('shape', 'square', 'xyzzy')
        self.put('shape', 'square', 'plugh')
        self.put('size', 'medium')
        expected = ['round', 'square', 'square', 'square']
        for batch_size in range(1, 6):
            assert self.get_all('shape', batch_size) == expected

    def test_get_details(self):
        print 'test_get_details()'
        self.put('one', '1')
        results, placemark = self.get_details('one')
        value, ttl, hash_type, secret_hash = results[0]
        assert value == '1'
        assert 0 < ttl <= 10
        assert hash_type == ''
        assert secret_hash == ''

        self.put('two', '2', 'secret')
        results, placemark = self.get_details('two')
        value, ttl, hash_type, secret_hash = results[0]
        assert value == '2'
        assert 0 < ttl <= 10
        assert hash_type == 'SHA'
        assert secret_hash == sha1('secret').digest()

    def test_put(self):
        print 'test_put()'
        self.put('answer', '42')
        self.put('answer', '42', ttl=20)
        assert self.get_all('answer') == ['42']
        results, placemark = self.get_details('answer')
        value, ttl, _, _ = results[0]
        assert ttl > 10

    def test_rm(self):
        print 'test_rm()'
        k = sha1('color').digest()
        v = sha1('green').digest()
        s = sha1('sesame').digest()

        self.put(k, v, s)
        assert self.get_all(k) == [v]

        self.rm(k, v, s)
        assert self.get_all(k) == []

    def get_all(self, key, batch_size=10):
        values = []
        placemark = ''
        while True:
            batch, placemark = self.get(key, batch_size, placemark)
            values.extend(batch)
            if not placemark:
                return sorted(values)

if __name__ == '__main__':
    p = Test(URL, ttl=10, preresolve=PRERESOLVE)
    p.test()
    print 'All tests passed.'
    print 'Please wait 10 seconds before rerunning.'
